package com.sxj.shop.servlet;

import com.sxj.shop.entity.Order;
import com.sxj.shop.entity.User;
import com.sxj.shop.exception.RequestParameterErrorException;
import com.sxj.shop.exception.UserNotLoginException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.OrderService;
import com.sxj.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("*.order")
public class OrderServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/create.order":
                createOrder(request,response);
                break;
            case "/findMyOrder.order":
                findMyOrder(request,response);
                break;
            case "/findDetail.order":
                findDetail(request,response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源不存在,请重新访问</h1>");
                break;
        }
    }
    protected void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
        User user = (User) request.getSession().getAttribute("user");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            String no = orderService.createNo(cart,user);
            request.getSession().setAttribute("no",no);
            response.sendRedirect(request.getContextPath()+"/jsp/success.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }
    protected void findMyOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
        User user = (User) request.getSession().getAttribute("user");
        try {
            List<Order> orders = orderService.findMyOrders(user);
            request.setAttribute("orders",orders);
            request.getRequestDispatcher("jsp/myOrders.jsp").forward(request,response);
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","服务器内部异常");
            request.getRequestDispatcher("/findAll.product").forward(request,response);
        }
    }

    protected void findDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
        String id = request.getParameter("id");
        try {
            Order order = orderService.findDetailById(id);
            request.setAttribute("order",order);
            request.getRequestDispatcher("/jsp/orderDetail.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("orderMsg",e.getMessage());
            request.getRequestDispatcher("/findMyOrder.order").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("orderMsg","服务器内部异常");
            request.getRequestDispatcher("/findMyOrder.order").forward(request,response);
        }
    }
}
